package com.sumc.springcloud.consumer.hystix;

import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;

/**
 * @describe：自定义的hystix的请求
 * @author: Smc
 * @version:v1.0 2020/3/20 17:20   Try to write good code
 */
public class MyHystix extends HystrixCommand<String> {

    private RestTemplate restTemplate;

    @Override
    protected String run() throws Exception {
        return restTemplate.getForEntity("http://SPRING-CLOUD-EUREKA-PROVIDER/say", String.class).getBody();
    }

    public MyHystix(Setter setter,RestTemplate restTemplate){
        super(setter);
        this.restTemplate= restTemplate;
    }

    /**
     * 降级熔断方法  超时，异常，不可用
      * @return
     */
    @Override
    public String getFallback(){
        Throwable executionException = super.getExecutionException();
        System.out.println(executionException.getMessage());
        System.out.println(executionException.getLocalizedMessage());

        return "error";
    }
}
